package com.hxzy.research.controller.adminresearch;

import com.hxzy.research.form.SubItemForm;
import com.hxzy.research.pojo.TblBigitem;
import com.hxzy.research.pojo.TblItem;
import com.hxzy.research.pojo.TblSelectvalue;
import com.hxzy.research.service.pageandmanager.FrontTeach;
import com.hxzy.research.service.research.ItemService;
import com.hxzy.research.vo.AdminResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class TeacherItemController {
    /**
     * 后台教学满意度的增删改查
     */
    @Autowired
    private FrontTeach frontTeach;
    @Autowired
    private ItemService itemService;
    @RequestMapping("/admin/goTeacherItem.go")
    public String goTeacherItem(Model model,Integer pageId)throws Exception{
        //先通过pageId查到大项
        List<TblBigitem> bigitems= frontTeach.findBigitemByPageid(pageId);  //大项
        //通过大项id查询小项
        for (TblBigitem bigitem:bigitems){
            List<TblItem> itemList= frontTeach.findItemByBigId(bigitem.getId());
            bigitem.setSubItemVOList(itemList);

            for (TblItem item:itemList){
                List<TblSelectvalue> selectvalueList= frontTeach.findSelectvalueById(item.getId());
                item.setSelectValueVOList(selectvalueList);

            }
        }
        model.addAttribute("itemList",bigitems);
        return "admin/research/teacherItem";
    }

    //跳转到添加页面
    @RequestMapping("/admin/goAddSubItem.go")
    public String goAddSubItem(Model model,Integer pageId, HttpSession session)throws Exception{
        //session.setAttribute("pageId",pageId);
        //通过pageId查询大项
        List<TblBigitem> bigitems= frontTeach.findBigitemByPageid(pageId);
        model.addAttribute("bigItemList",bigitems);
        return "admin/research/addSubItem";
    }
    //添加页面功能的提交
    @RequestMapping("/admin/addSubItem.go")
    public @ResponseBody AdminResultVo addSubItem(SubItemForm subItemForm)throws Exception{
        TblItem tblItem=new TblItem();
        //先添加小项
        BeanUtils.copyProperties(subItemForm,tblItem);
        itemService.addItem(tblItem);
        //查询小项id
        Integer id=itemService.findItemIdByName(tblItem).getId();
        System.out.println("************************************************"+id);
        //再添加满意值
        TblSelectvalue tblSelectvalue=new TblSelectvalue();
        List<Integer> valueList=subItemForm.getValueList();
        List<String> stringList= Arrays.asList("很满意","满意","一般","不满意","很不满意");
        //排序
        Collections.sort(valueList);
        Collections.reverse(valueList);
        //循环添加

        for (int i=0;i<5;i++){
            tblSelectvalue.setItemId(id);
            tblSelectvalue.setSelectValue(valueList.get(i));
            tblSelectvalue.setSelectName(stringList.get(i));
            itemService.addSelectValue(tblSelectvalue);
        }
        AdminResultVo adminResultVo=new AdminResultVo("OK");
        return adminResultVo;
    }

    //跳转到修改页面
    @RequestMapping("/admin/goUpdateSubItem.go")
    public String goUpdateSubItem(Model model,Integer id,Integer pageId)throws Exception{
        //通过pageId查询大项
        List<TblBigitem> bigitems= frontTeach.findBigitemByPageid(pageId);
        model.addAttribute("bigItemList",bigitems);
        //通过id查询小项的item_name
        TblItem subItem=itemService.findItemNameById(id);
        model.addAttribute("subItem",subItem);
        return "admin/research/updateSubItem";
    }
    //修改功能
    @RequestMapping("/admin/updateSubItem.go")
    public @ResponseBody AdminResultVo updateSubItem(SubItemForm subItemForm)throws Exception{
        //先修改小项
        TblItem tblItem=new TblItem();
        BeanUtils.copyProperties(subItemForm,tblItem);
        itemService.updateItem(tblItem);
        //然后修改满意值-先删除后添加
        //删除-通过小项id
        System.out.println("*****************************************"+tblItem.getId());
        itemService.deleteSelectvalue(tblItem.getId());


        TblSelectvalue tblSelectvalue=new TblSelectvalue();
        List<Integer> valueList=subItemForm.getValueList();
        List<String> stringList= Arrays.asList("很满意","满意","一般","不满意","很不满意");
        //排序
        Collections.sort(valueList);
        Collections.reverse(valueList);
        for (int i=0;i<5;i++){
            tblSelectvalue.setItemId(tblItem.getId());
            tblSelectvalue.setSelectValue(valueList.get(i));
            tblSelectvalue.setSelectName(stringList.get(i));
            itemService.addSelectValue(tblSelectvalue);
        }
        AdminResultVo adminResultVo=new AdminResultVo("OK");
        return adminResultVo;
    }
    //删除
    @RequestMapping("/admin/deleteTeachSubItem.go")
    public String deleteTeachSubItem(Model model,Integer id)throws Exception{
        //执行删除，先删除小项，再删除满意值表-也是通过小项id 小项id就是满意值表的item_id
        itemService.deleteItemById(id);
        itemService.deleteSelectvalue(id);
        return "redirect:goTeacherItem.go?pageId=1";
    }

}
